import java.util.Scanner;
class StackArray {
    private char[] stack;
    private int top;
    public StackArray(int size) {
        stack = new char[size];
        top = -1;
    }
    public boolean isEmpty() {
        return top == -1;
    }
    public boolean isFull() {
        return top == stack.length - 1;
    }
    public void push(char data) {
        if (isFull()) {
            System.out.println("栈满");
        } else {
            top++;
            stack[top] = data;
        }
    }
    public int pop() {
        if (isEmpty()) {
            System.out.println("栈空");
            return -1; // 返回空字符表示栈空
        } else {
            char data = stack[top]; // 获取栈顶元素
            top--; // 弹出栈顶元素
            return data; // 返回栈顶元素
        }
    }
}
public class Stack {
    public static boolean isValid(String s) {
        StackArray stack = new StackArray(s.length());
        for (char c : s.toCharArray()) {
            if (c == '(' || c == '[' || c == '{') {
                stack.push(c);
            } else if (c == ')' || c == ']' || c == '}') {
                if (stack.isEmpty()) {
                    System.out.println("括号不匹配");
                    return false;
                }
                char topChar = (char)stack.pop();
                if (c == ')' && topChar != '(') {
                    System.out.println("括号不匹配");
                    return false;
                } else if (c == ']' && topChar != '[') {
                    System.out.println("括号不匹配");
                    return false;
                } else if (c == '}' && topChar != '{') {
                    System.out.println("括号不匹配");
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入一个包含括号的字符串（或输入 exit 退出）：");
            String s = sc.nextLine();
            if (s.equals("exit")) {
                System.out.println("退出程序");
                break;
            }

            if (isValid(s)) {
                System.out.println("匹配成功");
            } else {
                System.out.println("匹配失败");
            }
        }
        sc.close();
    }
}
